#设置各种参数
typeset ORA_PRI_IP='10.27.5.199'               #主服务器ip
typeset ORA_SLA_IP='10.27.5.139'               #从服务器ip
typeset ORA_PRI_TNS_PORT='@@ORA_PRI_TNS_PORT@@'
typeset ORA_SLA_TNS_PORT='@@ORA_SLA_TNS_PORT@@'
typeset ORA_PORT='@@ORA_PORT@@'
typeset hmuser='oracle'                   #主服务器远程用户
typeset hmpassword='oracle'           #主服务器远程密码
typeset hsuser='oracle'                   #从服务器远程用户
typeset hspassword='oracle'           #从服务器远程密码
typeset check_times=''                  #命令重复次数
typeset ORA_SID='orcl'                       #设置主库SID
typeset ORA_SID_UP=$(echo "${ORA_SID}" | tr '[a-z]' '[A-Z]') #ORCL
typeset ORA_SID_DOWN=$(echo "${ORA_SID}" | tr '[A-Z]' '[a-z]')
typeset ssid='orcl'                       #设置从库SID
typeset ORA_PRI_DB_UNIQUE_NAME='orcl_pri'            #设置主库uniqname
typeset ORA_SLA_DB_UNIQUE_NAME='orcl_stl'            #设置从库uniqname
typeset ORA_PRI_SERVICE_NAME='@@ORA_PRI_SERVICE_NAME@@' #同orcl_pri
typeset ORA_SLA_SERVICE_NAME='@@ORA_SLA_SERVICE_NAME@@' #同orcl_stl
typeset ORA_PRI_DB_UNIQUE_NAME_UP=$(echo "${ORA_PRI_DB_UNIQUE_NAME}" | tr '[a-z]' '[A-Z]') #ORCL_PRI
typeset ORA_SLA_DB_UNIQUE_NAME_UP=$(echo "${ORA_SLA_DB_UNIQUE_NAME}" | tr '[a-z]' '[A-Z]') #ORCL_STL
typeset ORA_PRI_SERVICE_NAME_UP="${ORA_PRI_DB_UNIQUE_NAME_UP}" #同ORCL_PRI
typeset ORA_SLA_SERVICE_NAME_UP="${ORA_SLA_DB_UNIQUE_NAME_UP}" #同ORCL_STL
typeset ORA_USER='@@ORA_USER@@' #sys
typeset ORA_PASSWD='@@ORA_PASSWD@@' #sys
typeset ORA_ROLE='@@ORA_ROLE@@' # 分开写，则为常量primary或者standby
typeset ORA_BAK_HOME='@@ORA_BAK_HOME@@'

#配置日志路径
typeset SCRIPT_NAME="oracle_master_$ORA_PRI_IP"
typeset LOG_PATH='/tmp/dr_log'
[ ! -d "${LOG_PATH}" ] && mkdir -p ${LOG_PATH}
typeset LOG_FILE="${LOG_PATH}/${SCRIPT_NAME}.log"
[ ! -f "${LOG_FILE}" ] && touch ${LOG_FILE}

function log_format()
{
    echo "[`date +"%F %H:%M:%S"`] [$*]" | tee -a ${LOG_FILE}
}

log_format "begin to build oracle master................................."

#设置oracle环境变量
source /home/oracle/.bash_profile

#检查check次数
[ ! ${check_times} ] && typeset check_times='3'
expr 1 + ${check_times} &>/dev/null
if [ $? -ne 0 ];then
    log_format "ERROR" 'Argument needs to be integer';exit 1
fi

#获取oracle数据库状态
status=$(sqlplus / as sysdba <<eof |grep -w OPEN| sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'
select status from v\$instance;
eof
)
if [ "$status" != OPEN ];then
    log_format "ERROR" 'oracle database is not open !' && exit 1
else
    log_format "INFO" 'oracle database is open,continue...'
fi

#获取archive mode是否开启
mode=`echo 'archive log list;'|sqlplus / as sysdba|grep 'Archive Mode'|awk -F'\t' '{print $2}'|sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
echo $mode
if [ "$mode" != "Archive Mode" ];then
    log_format "WARNING" 'archive mode is not open'
    log_format "INFO" 'try to start archive mode'
    sqlplus / as sysdba <<eof | tee -a ${LOG_FILE}
shutdown immediate;
startup mount;
alter database archivelog;
alter database force logging;
alter database open;
eof
    if [ $? -ne 0 ];then
        log_format "ERROR" "sql command failed!" && exit 1
    fi
    mode=`echo 'archive log list;'|sqlplus / as sysdba|grep 'Archive Mode'|awk -F'\t' '{print $2}'|sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
    if [ "$mode" != "Archive Mode" ];then
        log_format "ERROR" 'fail to open archive mode' && exit 1
    else
        log_format "INFO" 'success to open archive mode'
    fi
else
    log_format "INFO" "archive mode is open"
fi

#创建standby redo log
log_format "INFO" "start to create standby redo log..."
#获取standby日志数量
sqlplus / as sysdba <<eof  > /tmp/vlogfile.txt
set linesize 300;
set pagesize 300;
column member format a60
select group#,member,type from v\$logfile;
eof
cat /tmp/vlogfile.txt | tee -a ${LOG_FILE}

#获取standby日志大小
sqlplus / as sysdba <<eof > /tmp/onlinelogsize.txt
select group#,bytes/1024/1024 as size_m from v\$log;
eof
cat /tmp/onlinelogsize.txt | tee -a ${LOG_FILE}


standbynum=`cat /tmp/vlogfile.txt|grep STANDBY|grep -v grep|wc -l`
onlinenum=`cat /tmp/vlogfile.txt|grep ONLINE|grep -v grep|wc -l`
if [ $standbynum -ne 0 ];then
    log_format "ERROR" "standbylog already exits,please check!" && exit 1
fi

maxsize=0
maxgroup=0
for i in `cat /tmp/onlinelogsize.txt |grep -A $(($onlinenum+1)) "SIZE_M"|tail -$onlinenum|awk '{print $2}'|sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
do
    if [ $i -gt "$maxsize" ];then
        maxsize=$i
    fi
done

for i in `cat /tmp/onlinelogsize.txt |grep -A $(($onlinenum+1)) "SIZE_M"|tail -$onlinenum|awk '{print $1}'|sed -e 's/^[ ]*//g' | sed -e 's/[ ]*$//g'`
do
    if [ $i -gt "$maxgroup" ];then
        maxgroup=$i
    fi
done
echo $maxsize
echo $onlinenum
echo $maxgroup

for ((i=1;i<=$(($onlinenum+1));i++))
do
let redonum=maxgroup+i
sqlplus / as sysdba <<eof | tee -a ${LOG_FILE}
ALTER DATABASE ADD STANDBY LOGFILE GROUP $redonum ('$ORACLE_BASE/oradata/${ORA_SID}/redo0${redonum}.log') size ${maxsize}M;
eof
done
if [ $? -ne 0 ];then
    log_format "ERROR" "failed to add standby log " && exit 1
else
    log_format "INFO" "success to run add standby log "
fi

#检查添加的日志组
log_format "INFO" "start to check standby log "
sqlplus / as sysdba <<eof > /tmp/stlogstat.txt
select group#,sequence#,bytes,status from v\$standby_log;
eof
cat /tmp/stlogstat.txt | tee -a ${LOG_FILE}
for ((i=1;i<=$(($onlinenum+1));i++))
do
let redonum=maxgroup+i
tmpnum=`cat /tmp/stlogstat.txt |grep -A $(($onlinenum+2)) STATUS|tail -$(($onlinenum+1))|awk '{print $1}'|grep -w $redonum|grep -v grep|wc -l`
if [ $tmpnum -ne 1 ];then
    log_format "ERROR" "redolog group $redonum add failed!" && exit 1
else
    log_format "INFO" "find redolog group $redonum"
fi
done
